import { actions } from './store';
import { put, takeEvery, all, call, takeLatest, select } from 'redux-saga/effects';

async function wait(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

function* subA(name) {
  yield call(wait, 500);
  yield select((state) => state);
  yield put(actions.add('A:---(1)-' + name));
  yield call(wait, 800);
  yield select((state) => state);
  yield put(actions.add('A:---(2)-' + name));
  yield call(wait, 1000);
  yield select((state) => state);
  yield put(actions.add('A:---(3)-' + name));
  yield call(wait, 1000);
  yield select((state) => state);
}

function* A() {
  const name = Date.now().toString(20).substring(6, 10);
  yield select((state) => state);
  yield put(actions.add('A start:' + name));
  yield subA(name);
  yield put(actions.add('A end:' + name));
}

export default function* mySaga() {
  yield takeLatest(actions.setFilter.type, A);
  // yield takeEvery(actions.toggle.type, function* () {
  //   console.log('every');
  //   yield 1;
  // });
}
